package com.wy.business.market.apps.actions;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hamcrest.core.IsNull;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.market.apps.services.AppManageService;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.exceptions.ServiceException;

/**
 * 项目名称：openportal
 * 类名称：AppManageAction
 * 类描述：我的APP仓库_APP管理
 * 创建人：张旭星
 * 创建时间：2016-3-21 上午11:45:33
 * 修改人：张旭星
 * 修改时间：2016-3-21 上午11:45:33
 * 版本信息：@version 1.0
 * Copyright (c) 2016万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@ParentPackage("json")
@Namespace("/apps/appManage")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class AppManageAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(AppManageAction.class);

	@Autowired
	private AppManageService appManageService;
	
	private List<Map> rows;
	private List<Map> orgInfo;
	private String id;

	/**
	 * Function Name               list                                   
	 * @return
	 * @throws ServiceException          		             
	 * @description APP管理
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-4 下午1:46:43           张旭星                     Initial
	 **********************************************************************
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value = "list", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String list() throws ServiceException {
		try {
			rows = appManageService.list();
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	
	@Action(value = "myListChecked", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String myListChecked(){
		String tab = ServletActionContext.getRequest().getParameter("tab");
		try {
			rows = appManageService.myListChecked(tab);
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	@Action(value = "myListCheck", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String myListCheck(){
		try {
			rows = appManageService.myListCheck();
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value = "myListAudited", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String myListAudited(){
		String source = ServletActionContext.getRequest().getParameter("source");
		try {
			rows = appManageService.myListAudited(source);
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value = "myListToDo",
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String myListToDo(){
		try {
			rows = appManageService.myListToDo();
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value = "myListMarketNews", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String myListMarketNews(){
		try {
			rows = appManageService.myListMarketNews();
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value = "myListLoginLink", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String myListLoginLink(){
		try {
			rows = appManageService.myListLoginLink();
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	@Action(value = "queryAppVersion", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) })
	public String queryAppVersion() throws ErrorSqlException{
		String functionId=ServletActionContext.getRequest().getParameter("functionId");
		String versionId=ServletActionContext.getRequest().getParameter("versionId");
		rows=appManageService.queryAppVersion(functionId,versionId);
		Map a=new HashMap();
		a.put("screenshots", appManageService.queryAppScreenshot(functionId));
		rows.add(a);
		return SUCCESS;
	}
	
	
	@SuppressWarnings("unchecked")
	@Action(value = "queryAppVersionNoList", 
	interceptorRefs = { @InterceptorRef("sessionStack") }, 
	results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) })
	public String queryAppVersionNoList() throws ErrorSqlException{
		String functionId=ServletActionContext.getRequest().getParameter("functionId");
		rows=appManageService.queryAppVersionNoList(functionId);
		return SUCCESS;
	}
	
	
	@SuppressWarnings("unchecked")
	@Action(value = "updateAppVersion", 
	interceptorRefs = { @InterceptorRef("sessionStack") }, 
	results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String updateAppVersion() throws ErrorSqlException{
		String functionId=ServletActionContext.getRequest().getParameter("functionId");
		String versionId=ServletActionContext.getRequest().getParameter("versionId");
		try {
			appManageService.updateAppVersion(functionId,versionId);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	
	@Action(value = "updateAppUpDownState", 
	interceptorRefs = { @InterceptorRef("sessionStack") }, 
	results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
	"false"}) })
	public String updateAppUpDownState() throws ErrorSqlException{
		String functionId=ServletActionContext.getRequest().getParameter("functionId");
		String state=ServletActionContext.getRequest().getParameter("state");
		String reason=ServletActionContext.getRequest().getParameter("reason");
		appManageService.updateAppUpDownState(functionId,state,reason);
		return SUCCESS;
	}
	
	
	@Action(value = "queryAppScreenshot", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) })
	public String queryAppScreenshot() throws ErrorSqlException{
		String functionId=ServletActionContext.getRequest().getParameter("functionId");
		rows=appManageService.queryAppScreenshot(functionId);
		return SUCCESS;
	}
	/*
	 * 查询所有App应用分类*/
	@Action(value = "queryAppList", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) })
	public String queryAppList() throws ErrorSqlException{
		rows=appManageService.queryAppList();
		return SUCCESS;
	}
	/*
	 * 查询所有App应用分类*/
	@Action(value = "queryAllApp", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) })
	public String queryAllApp() throws ErrorSqlException{
		String categoryid=ServletActionContext.getRequest().getParameter("categoryId");
		rows=appManageService.queryAllApp(categoryid);
		return SUCCESS;
	}
	@Action(value = "initAllApp", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) })
	public String initAllApp() throws ErrorSqlException{
		//String categoryid=ServletActionContext.getRequest().getParameter("categoryId");
		rows=appManageService.initAllApp();
		return SUCCESS;
	}
	/**
	 * 
	 * Function Name               deleteApp                                   
	 * @return
	 * @throws ErrorSqlException          		             
	 * @description 删除一个APP
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-3-31 下午2:09:48           田松林                   Initial
	 **********************************************************************
	 */
	@Action(value = "deleteApp", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String deleteApp() throws ErrorSqlException{
		String functionId = ServletActionContext.getRequest().getParameter("functionId");
		try {
			String versionId = appManageService.queryPreVersionId(functionId);
			if (StringUtils.isNotBlank(versionId)) {
				appManageService.updateAppVersion(functionId, versionId);
			} else {
				appManageService.deleteApp(functionId);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public List<Map> getRows() {
		return rows;
	}

	public List<Map> getOrgInfo() {
		return orgInfo;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getTotal() {
		if (rows == null) {
			return 0;
		} else {
			return rows.size();
		}
	}

}
